package cn.edu.zut.internship.web.config;

import cn.edu.zut.internship.web.shiro.CustomRealm;
import cn.edu.zut.internship.web.shiro.RedisCache;
import cn.edu.zut.internship.web.shiro.RedisCacheManager;
import cn.edu.zut.internship.web.shiro.RedisSessionDAO;
import cn.edu.zut.internship.web.shiro.filter.PermissionAuthFilter;
import cn.edu.zut.internship.web.shiro.filter.TokenAuthFilter;
import cn.edu.zut.internship.web.utils.MySessionManager;
import cn.edu.zut.internship.web.utils.RedisUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.servlet.SimpleCookie;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.HashMap;
import java.util.Map;

@Configuration
class shiroConfig {
    @Autowired
    RedisSessionDAO redisSessionDAO;
    @Autowired
    RedisCacheManager redisCacheManager;

    @Bean
    @ConditionalOnMissingBean
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
        DefaultAdvisorAutoProxyCreator defaultAAP = new DefaultAdvisorAutoProxyCreator();
        defaultAAP.setProxyTargetClass(true);
        return defaultAAP;
    }

    //将自己的验证方式加入容器
    @Bean
    public CustomRealm myShiroRealm() {
        CustomRealm customRealm = new CustomRealm();
        return customRealm;
    }

    //权限管理，配置主要是Realm的管理认证
    @Bean
    public SecurityManager securityManager() {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(myShiroRealm());
        securityManager.setSessionManager(getDefaultWebSessionManager(redisSessionDAO,redisCacheManager));
        //给SecurityUtils全局安全工具类设置SecurityManger
        SecurityUtils.setSecurityManager(securityManager);
        return securityManager;
    }
    /**
     * 配置shiro session 的一个管理器
     */
    @Bean(name = "sessionManager")
    public DefaultWebSessionManager getDefaultWebSessionManager(RedisSessionDAO redisSessionDAO, RedisCacheManager redisCacheManager) {
        DefaultWebSessionManager sessionManager = new MySessionManager();
        //session有效期 默认值1800000 30分钟 1800000毫秒  -1000表示永久
        sessionManager.setGlobalSessionTimeout(3600000L*12);
        sessionManager.setSessionDAO(redisSessionDAO);
        sessionManager.setCacheManager(redisCacheManager);
        SimpleCookie simpleCookie = getSimpleCookie();
        //设置js可读取此Cookie
        simpleCookie.setHttpOnly(false);
        simpleCookie.setPath("/");
        sessionManager.setSessionIdCookie(simpleCookie);
        return sessionManager;
    }

    //Filter工厂，设置对应的过滤条件和跳转条件
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        Map<String, String> map = new HashMap<>();
        //登出
        map.put("/logout", "logout");
        //对所有用户认证
        map.put("/**", "authc");
        //无需认证
        map.put("/query","anon");
        //登录
        shiroFilterFactoryBean.setLoginUrl("/auth/login");
        //首页
        shiroFilterFactoryBean.setSuccessUrl("/index");
        //错误页面，认证不通过跳转
        shiroFilterFactoryBean.setUnauthorizedUrl("/error");
        // 创建一个Map对象包含所有要集成到Shiro框架中去的filter对象
        // 注册哪些过滤器
        Map<String, Filter> filters = new HashMap<String, Filter>();
        // <filter>
        // <filter-mapping
        TokenAuthFilter tokenAuthFilter = new TokenAuthFilter();
        tokenAuthFilter.setRedisUtil(new RedisUtils());
        PermissionAuthFilter permissionAuthFilter = new PermissionAuthFilter();
        filters.put("authcToken", tokenAuthFilter);
        filters.put("permAuthc", permissionAuthFilter);

        // 在shiro中注册过滤器
        shiroFilterFactoryBean.setFilters(filters);
        shiroFilterFactoryBean.setFilterChainDefinitionMap(map);
        return shiroFilterFactoryBean;
    }
    @Bean
    public SimpleCookie getSimpleCookie() {
        SimpleCookie simpleCookie = new SimpleCookie();
        simpleCookie.setName("INTERNSHIP_SSIONID");
        return simpleCookie;
    }
    /**
     * RedisSessionDao
     * @return
     */
    @Bean
    public RedisSessionDAO getRedisSessionDao() {
        RedisSessionDAO redisSessionDAO = new RedisSessionDAO();
        return redisSessionDAO;
    }
    /**
     * RedisCache
     * @return
     */
    @Bean
    public RedisCache getRedisCache() {
        RedisCache redisCache = new RedisCache();
        return redisCache;
    }
    /**
     * redisCacheManager
     * @auth 011336
     * @DATE 2018/10/15
     * @return
     */
    @Bean
    public RedisCacheManager getRedisCacheManager(RedisCache redisCache) {
        RedisCacheManager redisCacheManager = new RedisCacheManager();
        redisCacheManager.setRedisCache(redisCache);
        return redisCacheManager;
    }

    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
        return authorizationAttributeSourceAdvisor;
    }
}
